//
//  AudioClassifyListVC.m
//  LiteraryTune
//
//  Created by Mr.Xy on 2018/11/28.
//  Copyright © 2018 cxd. All rights reserved.
//

#import "AudioClassifyListVC.h"
#import "AudioClassifyModel.h"
#import "BookCollectCell.h"
#import "VideoDetailVC.h"

@interface AudioClassifyListVC ()<UICollectionViewDelegate, UICollectionViewDataSource, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

@property (strong, nonatomic) UICollectionView *collectionView;

@property (strong, nonatomic) NSArray<AudioClassifyItemModel *> *modelArray;

@end

@implementation AudioClassifyListVC
{
    BOOL _requestCompelted;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.mediaName;
 
    [self setupUI];
    
    [self getNetData];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (void)setupUI {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.itemSize = CGSizeMake((kScreenWidth - 10) / 3, 150);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.emptyDataSetDelegate = self;
    self.collectionView.emptyDataSetSource = self;
    self.collectionView.backgroundColor = [UIColor colorFromHexCode:@"#eeeeee"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"BookCollectCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.collectionView.mj_footer = footer;
    [self.view addSubview:self.collectionView];
}

- (void)loadData {
    
    
}

- (void)loadMoreData {
    
    
}

- (void)getNetData {
    
    NSString *urlString = [NSString stringWithFormat:@"http://www.wenqujingdian.com/index.php/jiekou/getAudioByClassId/cid/%@", self.mediaId];
    _requestCompelted = NO;
    [CXDNetHelp getRequestWithURLString:urlString parameters:nil success:^(id  _Nullable responseObject) {
        _requestCompelted = YES;
        self.modelArray = [AudioClassifyItemModel mj_objectArrayWithKeyValuesArray:responseObject];
        [self.collectionView reloadData];
        [self.collectionView reloadEmptyDataSet];
    } failure:^(NSError * _Nullable error) {
        _requestCompelted = YES;
    }];
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return _requestCompelted && self.modelArray.count == 0;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backView.hidden = YES;
    [cell.bookImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", RootHomeURL, self.modelArray[indexPath.item].spic]]];
    cell.nameLabel.text = self.modelArray[indexPath.item].yinpinname;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    VideoDetailVC *detailVC = [[VideoDetailVC alloc] init];
    AudioClassifyItemModel *tempModel = self.modelArray[indexPath.item];
    detailVC.navigationItem.title = tempModel.yinpinname;
    detailVC.videoId = tempModel.yinpinId;
    [self.navigationController pushViewController:detailVC animated:YES];
}


@end
